package codename.carrot.bomberman.views.widgets;

import sheep.graphics.Image;
import sheep.math.BoundingBox;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.MotionEvent;
import codename.carrot.bomberman.AppStorage;
import codename.carrot.bomberman.BitmapFactory;
import codename.carrot.bomberman.AppStorage.Direction;

/**
 *  Widget which allows player to switch camera-lock-on to other players, when dead
 *  @author Group A4
 */

public class PlayerDeadWidget extends BombermanWidget { 
    /**
     *  Enum with directions in which the player can switch camera-lock-on (to view other players when dead)
     */
    public enum ToggleDirection {LEFT, RIGHT};
    /**
     * The image for the toggle-button for left direction
     */
    private Image left = BitmapFactory.buildToggleButton(Direction.LEFT), 
    /**
     * The image for the toggle-button for left direction
     */
        right = BitmapFactory.buildToggleButton(Direction.RIGHT);
    /**
     *  The text to be displayed at the top of the widget
     */
    private String message = "You are dead...";
    /**
     * The boundingbox for the left togglebutton
     */
    private BoundingBox leftBounds, 
    /**
     * The boundingbox for the right togglebutton
     */
    rightBounds;
    
    /**
     * Visibility of this widget
     */
    private boolean isVisible = false;
    
    public PlayerDeadWidget(){
        this.leftBounds = this.setBounds(Direction.LEFT);
        this.rightBounds = this.setBounds(Direction.RIGHT);
    }

    /**
     * Sets this widget to be visible of <code>isVisible</code> is true, not visible if false
     * @param isVisible
     */
    public void setVisible(boolean isVisible){
        this.isVisible = isVisible;
    }
    
    /**
     *  Returns true if this widget is visible, false if not
     * @return isVisible
     */
    public boolean isVisible() {
        return isVisible;
    }
    
    /**
     * Sets and returns the boundingbox for a togglebutton based on direction <code>d</code>.
     * @return BoundingBox
     */
    private BoundingBox setBounds(Direction direction){
        Rect bounds = new Rect();
        if(direction == Direction.LEFT){
            bounds.left = 0;
            bounds.top = (int)((AppStorage.SCREEN_HEIGHT_GAME/2)-(left.getHeight()/2));
            bounds.right = (int)left.getWidth();
            bounds.bottom = (int)((AppStorage.SCREEN_HEIGHT_GAME/2)+(left.getHeight()/2));
        } else {
            bounds.left = (int)(AppStorage.SCREEN_WIDTH_GAME-right.getWidth());
            bounds.top = (int)((AppStorage.SCREEN_HEIGHT_GAME/2)-(right.getHeight()/2));
            bounds.right = (int)(AppStorage.SCREEN_WIDTH_GAME);
            bounds.bottom = (int)((AppStorage.SCREEN_HEIGHT_GAME/2)+(right.getHeight()/2));
        }
        return new BoundingBox(bounds);
    }
    
    
    @Override
    public boolean onTouchDown(MotionEvent event) {
        super.onTouchDown(event);
        return true;
    }
    
    @Override
    public boolean onTouchUp(MotionEvent event) {
        if(!isVisible) return false;
        if(leftBounds.contains(event.getX(), event.getY())){
            alertListeners(new PlayerDeadWidgetAction(this, ToggleDirection.LEFT));            
            return true;
        }
        if(rightBounds.contains(event.getX(), event.getY())){             
            alertListeners(new PlayerDeadWidgetAction(this, ToggleDirection.RIGHT));            
            return true;
        }
        return false;
    }

    @Override
    public void draw(Canvas canvas) {
    	if(canvas == null) return;
        if(isVisible){
            canvas.drawColor(Color.argb(127, 0, 0, 0));
            canvas.drawText(message, (AppStorage.SCREEN_WIDTH_GAME/2)-this.getTextPaint().measureText(message)/2, AppStorage.SCREEN_HEIGHT_GAME/2, this.getTextPaint());
            left.draw(canvas, 0, (AppStorage.SCREEN_HEIGHT_GAME/2)-(left.getHeight()/2));
            right.draw(canvas, AppStorage.SCREEN_WIDTH_GAME-left.getWidth(), (AppStorage.SCREEN_HEIGHT_GAME/2)-(left.getHeight()/2));
        }
    }
    
    
    /**
     * For painting white text with a size of 60 px.    
     * @return The paint.
     */
    private Paint getTextPaint(){
        Paint p = new Paint();
        p.setTextSize(60);
        p.setColor(Color.WHITE);
        return p;
    }

}
